package edu.uc.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import edu.uc.bean.Goods;
import edu.uc.dao.GoodsDao;

@Repository("goodsDao")
public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements GoodsDao{

	
	public Long insert(Goods bean) {
		Long result = 0L;

		try {
			// 保存
			getTmpl().save(bean);

			// 获取主键
			result = bean.getGoodId();
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Long delete(Long id) {
		Long result = 0L;

		try {
			// 使用会话,获取特定主键的某个类型的对象
			Goods instance = getTmpl().get(Goods.class, id);
			// 如果存在,就删除
			if (instance != null) {
				getTmpl().delete(instance);
			}

			// 正常用提交,且数据存在, 则说明删除成功
			if (instance != null) {
				result = 1L;
			}
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	
	public Long update(Goods bean) {
		Long result = 0L;

		try {
			getTmpl().update(bean);

			// 正常用提交,且数据存在, 则说明删除成功
			result = 1L;
		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	
	public Goods load(Long id) {
		Goods bean = null;

		try {
			// 使用会话,获取特定类型的主键对象
			bean = getTmpl().get(Goods.class, id);

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return bean;
	}

	
	public List<Goods> list() {
		List<Goods> vList = new ArrayList<Goods>();
		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Goods ");
		sbHql.append(" order by goodId desc ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();
		try {

			vList = (List<Goods>) getTmpl().find(hql, params);

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}
		return vList;
	
	}

	@Override
	public Long count() {
		Long result = 0L;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from Goods ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Long countByName(String name) {
		Long result = 0L;

		name = "%" + name + "%";

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from Goods where name like ? ");

		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {

			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return result;
	}

	@Override
	public Goods loadByName(String name) {
		Goods bean = null;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Goods where name=? ");
		sbHql.append(" order by goodId desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);
		Object[] params = arrParams.toArray();
		try {

			bean = getTmpl().execute(this.getCallbackBean(Goods.class, hql, params));

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}

		return bean;
	}

	@Override
	public List<Goods> pager(Long pageNum, Long pageSize) {
		List<Goods> vList = new ArrayList<Goods>();

		Long vStart = 0L;
		// Long vEnd = 0;

		vStart = (pageNum - 1) * pageSize;// 下标从0开始,所以不要+1
		// vEnd = pageNum * pageSize;
		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Goods ");
		sbHql.append(" order by goodId asc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		
		Object[] params = arrParams.toArray();

		try {

			vList = getTmpl().execute(this.getCallbackPager(Goods.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);

		} finally {

		}

		return vList;
	}

	@Override
	public List<Goods> pagerByName(String name, Long pageNum, Long pageSize) {
		List<Goods> vList = new ArrayList<Goods>();

		Long vStart = 0L;
		// Long vEnd = 0L;

		vStart = (pageNum - 1) * pageSize;// 不要+1
		// vEnd = pageNum * pageSize;

		name = "%" + name + "%";

		Long startIndex = vStart;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from Goods ");
		sbHql.append(" where name like ? ");
		sbHql.append(" order by goodId desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {
			vList = getTmpl().execute(this.getCallbackPager(Goods.class, startIndex, pageSize, hql, params));

		} catch (Exception e) {

			throw new RuntimeException(e);
		} finally {

		}

		return vList;
	}

	
}
